{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from sklearn import preprocessing, neighbors, svm\n",
    "from sklearn.cluster import KMeans\n",
    "from sklearn.model_selection import train_test_split\n",
    "import pandas as pd\n",
    "import warnings\n",
    "from matplotlib import pyplot as plt\n",
    "from matplotlib import style\n",
    "from collections import Counter\n",
    "import pickle\n",
    "import random\n",
    "style.use('fivethirtyeight')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.857142857143\n"
     ]
    }
   ],
   "source": [
    "# Preparing Data\n",
    "df = pd.read_csv('Feature_all_optomized.csv')\n",
    "\n",
    "# define train and test data\n",
    "# X = np.array(df.drop(['class'], 1)) # put every thing in array except 'class' label\n",
    "Y = np.array(df['class'])  # array have 'class' only\n",
    "fil = open('GaussianRandomProjection.pickle', 'rb')\n",
    "X = pickle.load(fil)\n",
    "fil.close()\n",
    "# split data in train and test groups\n",
    "x_train, x_test, y_train, y_test = train_test_split(X, Y, test_size = 0.4)\n",
    "\n",
    "# implementing the k-nearest neighbors classifier\n",
    "clf = neighbors.KNeighborsClassifier()\n",
    "clf.fit(x_train, y_train)\n",
    "# classifier_f = open('KN_algo.pickle','wb')\n",
    "# pickle.dump(clf, classifier_f)\n",
    "# classifier_f.close()    \n",
    "# test the accuracy of algorithm|\n",
    "accuracy = clf.score(x_test, y_test)\n",
    "print(accuracy)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# save classifier\n",
    "classifier_f = open('KN_algo.pickle','wb')\n",
    "pickle.dump(clf, classifier_f)\n",
    "classifier_f.close()    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# make a prediction\n",
    "\n",
    "ex_measure = np.array([0.185655,157.536530,123.564873,13242.350111,0.566160,52.502389,0.430877, 0.653675,1.391882,5.375214])\n",
    "# print(ex_measure)\n",
    "        \n",
    "ex_measure = ex_measure.reshape(1, -1)\n",
    "# print(ex_measure)\n",
    "prediction = clf.predict(ex_measure)\n",
    "\n",
    "print(prediction)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## SVM Classifier"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.857142857143\n"
     ]
    }
   ],
   "source": [
    "# implementing the svm classifier\n",
    "clf1 = svm.SVC()\n",
    "clf1.fit(x_train,y_train)\n",
    "\n",
    "# test the accuracy of algorithm\n",
    "accuracy = clf1.score(x_test, y_test)\n",
    "print(accuracy)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# save classifier\n",
    "classifier_f = open('SVM.pickle','wb')\n",
    "pickle.dump(clf1, classifier_f)\n",
    "classifier_f.close()    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1]\n"
     ]
    }
   ],
   "source": [
    "ex_measure = np.array([0.185655,157.536530,123.564873,13242.350111,0.566160,52.502389,0.430877, 0.653675,1.391882,5.375214])\n",
    "# print(ex_measure)\n",
    "        \n",
    "ex_measure = ex_measure.reshape(1, -1)\n",
    "# print(ex_measure)\n",
    "prediction = clf1.predict(ex_measure)\n",
    "\n",
    "print(prediction)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## KMeans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAZsAAAEJCAYAAABCNoqwAAAABHNCSVQICAgIf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k2b\nNq3jmmVZqqio0JYtW/TJJ59oyJAhLlbovMzMTI0dO7bLtTFjxuijjz5yqSLzYiJs0tPTlZ6efsb3\n3XXXXbr11lu7XLvssst03333afbs2abKM2KgfZba79HMnTtXtm3rueee03nnnWe4ushJSUnRpZde\nqj179mjOnDkd1/fs2aMf/vCHLlZm1sqVK7Vr1y7t3r1bY8aMcbsco2bPnq2JEyd2ubZs2TJlZ2dr\n+fLlSklJcakyc6ZNm6ZDhw51uXbo0CGNHDnSpYrMi4mwGagLL7xQF154YY/rI0aMiNvfDI8fP66S\nkhIdP35c27ZtUzgcVjgcliQNGzYsLv5HXbZsmW6++WZNnjxZU6dO1ZYtW3T06FEtWLDA7dKMuOOO\nO7R9+3Y988wzSktLU11dnSQpEAjE1S8Sp6WlpfVY/OD3+zVs2DDl5ua6VJVZS5cu1VVXXaUNGzao\npKREVVVV2rx5s+666y63SzMmrsImEb311ls6ePCgJGny5MldXut+TydWlZSU6NixY1q/fr3q6uo0\nfvx47dixQ6NGjXK7NCPKysokqcdy/ZUrV2r16tVulASHTZo0Sdu2bVNpaanWr1+vESNGaM2aNVq0\naJHbpRkTl8/ZAACiS9wtfQYARB/CBgBgHGEDADCOsAEAGEfYAACMI2wAAMYRNgAA4wgbAIBxhA0A\nwLj/D+JcAwzlpzjwAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7fe4cbe38bd0>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "clf =  KMeans(n_clusters =2)\n",
    "clf.fit(X)\n",
    "centroids = clf.cluster_centers_\n",
    "labels = clf.labels_\n",
    "\n",
    "colors =10* [\"g.\", \"r.\",\"c.\",\"b.\",\"k.\"]\n",
    "\n",
    "for i in range(len(X)):\n",
    "    plt.plot(X[i][0] , X[i][1], colors[labels[i]], markersize=10)\n",
    "plt.scatter(centroids[:,0],centroids[:,1], marker='x', s=150, linewidths=5)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.529411764706\n"
     ]
    }
   ],
   "source": [
    "# data = preprocessing.scale(X)\n",
    "data = X\n",
    "clf = KMeans(n_clusters=2)\n",
    "clf.fit(data)\n",
    "# print(clf)\n",
    "\n",
    "correct = 0\n",
    "for i in range(len(data)):\n",
    "    predict_me = np.array(data[i].astype(float))\n",
    "    predict_me = predict_me.reshape(-1 , len(predict_me))\n",
    "    #     print(predict_me)\n",
    "    prediction = clf.predict(predict_me)\n",
    "    if prediction[0] == Y[i]:\n",
    "        correct +=1\n",
    "        \n",
    "print(correct / float(len(X)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# save classifier\n",
    "classifier_f = open('Kmean.pickle','wb')\n",
    "pickle.dump(clf, classifier_f)\n",
    "classifier_f.close()    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# new data with Feature Reduction\n",
    "\n",
    "fil = open('pca_trans_data.pickle', 'rb')\n",
    "data = pickle.load(fil)\n",
    "fil.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.470588235294\n"
     ]
    }
   ],
   "source": [
    "clf = KMeans(n_clusters=2)\n",
    "clf.fit(data)\n",
    "# print(clf)\n",
    "\n",
    "correct = 0\n",
    "for i in range(len(data)):\n",
    "    predict_me = np.array(data[i].astype(float))\n",
    "    predict_me = predict_me.reshape(-1 , len(predict_me))\n",
    "    #     print(predict_me)\n",
    "    prediction = clf.predict(predict_me)\n",
    "    if prediction[0] == Y[i]:\n",
    "        correct +=1\n",
    "        \n",
    "print(correct / float(len(X)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.0\n"
     ]
    }
   ],
   "source": [
    "fil = open('y.pickle', 'rb')\n",
    "Y = pickle.load(fil)\n",
    "fil.close()\n",
    "x_train, x_test, y_train, y_test = train_test_split(data, Y, test_size = 0.3)\n",
    "# implementing the k-nearest neighbors classifier\n",
    "clf = neighbors.KNeighborsClassifier()\n",
    "clf.fit(x_train, y_train)\n",
    "classifier_f = open('KN_algo.pickle','wb')\n",
    "pickle.dump(clf, classifier_f)\n",
    "classifier_f.close()    \n",
    "# test the accuracy of algorithm|\n",
    "accuracy = clf.score(x_test, y_test)\n",
    "print(accuracy)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.857142857143\n"
     ]
    }
   ],
   "source": [
    "# implementing the svm classifier\n",
    "clf1 = svm.SVC()\n",
    "clf1.fit(x_train,y_train)\n",
    "\n",
    "# test the accuracy of algorithm\n",
    "accuracy = clf1.score(x_test, y_test)\n",
    "print(accuracy)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
